import base64
import datetime
import hashlib
import json
import os
import random
import ssl
import time
import uuid


def get_ip(request):
    if 'HTTP_X_FORWARDED_FOR' in request.META:
        ip = request.META.get('HTTP_X_FORWARDED_FOR')
    else:
        ip = request.META.get('REMOTE_ADDR')

    return ip


def get_json_values(key, data, default=None):
    ret = data.get(key, default)
    return ret


def get_time_uuid():
    """
        :return: 20220525140635467912
        :PS ：并发较高时尾部随机数增加
    """
    time_uuid = str(datetime.datetime.fromtimestamp(time.time())).replace("-", "").replace(" ", "").replace(":","").replace(".", "") + str(random.randint(100, 999))
    return time_uuid


# 获取文件后缀
def get_file_type(file):
    file_type = os.path.splitext(file)[-1]
    filename_name = os.path.splitext(file)[0]
    return filename_name, file_type

# 获取文件后缀
def get_file_type1(file):
    """
     uniapp需要这样获取
    :param file:
    :return:
    """
    file_type = file.name.split(".")[-1]
    filename_name = file.name.split(".")[0]
    return filename_name, file_type


def file_save(file, mediaPath, save_path):
    """

    :param file:
    :param mediaPath:
    :param save_path:
    :return:
    """
    filename_name, file_type = get_file_type1(file)
    save_name = get_time_uuid()
    path = mediaPath + save_path + save_name+"."+file_type
    with open(path, 'wb+') as temp_file:
        for chunk in file.chunks():
            temp_file.write(chunk)
    return filename_name, save_path + save_name+"."+file_type

def get_uuid():
    get_timestamp_uuid = uuid.uuid1()  # 根据 时间戳生成 uuid , 保证全球唯一
    return get_timestamp_uuid

def get_time_uid():
    """
        :return: 20220525140635467912
        :PS ：并发较高时尾部随机数增加
    """
    string_id = str(datetime.datetime.fromtimestamp(time.time())).replace("-", "").replace(" ", "").replace(":","").replace(".", "") + str(random.randint(100, 999))
    return string_id

def delete_file(path):
    """
    删除文件
    """
    try:
        os.remove(path)
        ret = 1
    except Exception:
        ret = 0
    return ret
def get_time_to_file_name():
    ct = time.time()
    local_time = time.localtime(ct)
    data_head = time.strftime("%Y%m%d%H%M%S", local_time)
    data_secs = (ct - int(ct)) * 10000
    fn = "%s%03d" % (data_head, data_secs)  # 17位时间戳
    return fn


def get_now_datetime_string():
    ct = time.time()
    local_time = time.localtime(ct)
    data_head = time.strftime("%Y%m%d%H%M%S", local_time)
    return data_head


def bytes_2_bs64(img_byte):
    b64 = base64.b64encode(img_byte)
    bs64_str = b64.decode('utf-8')
    return bs64_str

from rest_framework.response import Response
from tools.encryption import setDataAes

class MyResponse(Response):
    def __init__(self, data=None, serect='wchime77520'):
        super(MyResponse, self).__init__()
        self.data = setDataAes(serect, json.dumps(data))


if __name__ == "__main__":

    pass




